/**
 * NEURON IIoT System for Industry 4.0
 * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/

/*
 * DO NOT EDIT THIS FILE MANUALLY!
 * It was automatically generated by `json-autotype`.
 */

#include <stdlib.h>
#include <string.h>

#include "json/json.h"

#include "json/neu_json_rw.h"

int neu_json_encode_read_resp(void *json_object, void *param)
{
    int                   ret  = 0;
    neu_json_read_resp_t *resp = (neu_json_read_resp_t *) param;

    void *                    tag_array = neu_json_array();
    neu_json_read_resp_tag_t *p_tag     = resp->tags;
    for (int i = 0; i < resp->n_tag; i++) {
        neu_json_elem_t tag_elems[2] = { 0 };

        tag_elems[0].name      = "name";
        tag_elems[0].t         = NEU_JSON_STR;
        tag_elems[0].v.val_str = p_tag->name;

        if (p_tag->error != 0) {
            tag_elems[1].name      = "error";
            tag_elems[1].t         = NEU_JSON_INT;
            tag_elems[1].v.val_int = p_tag->error;
        } else {
            tag_elems[1].name = "value";
            tag_elems[1].t    = p_tag->t;
            tag_elems[1].v    = p_tag->value;
        }

        tag_array = neu_json_encode_array(tag_array, tag_elems,
                                          NEU_JSON_ELEM_SIZE(tag_elems));
        p_tag++;
    }

    neu_json_elem_t resp_elems[] = { {
        .name         = "tags",
        .t            = NEU_JSON_OBJECT,
        .v.val_object = tag_array,
    } };
    ret = neu_json_encode_field(json_object, resp_elems,
                                NEU_JSON_ELEM_SIZE(resp_elems));

    return ret;
}

int neu_json_encode_read_resp1(void *json_object, void *param)
{
    int                   ret  = 0;
    neu_json_read_resp_t *resp = (neu_json_read_resp_t *) param;

    void *                    values = neu_json_encode_new();
    void *                    errors = neu_json_encode_new();
    neu_json_read_resp_tag_t *p_tag  = resp->tags;
    for (int i = 0; i < resp->n_tag; i++) {
        neu_json_elem_t tag_elem = { 0 };

        if (p_tag->error == 0) {
            tag_elem.name = p_tag->name;
            tag_elem.t    = p_tag->t;
            tag_elem.v    = p_tag->value;
            neu_json_encode_field(values, &tag_elem, 1);
        } else {
            tag_elem.name      = p_tag->name;
            tag_elem.t         = NEU_JSON_INT;
            tag_elem.v.val_int = p_tag->error;
            neu_json_encode_field(errors, &tag_elem, 1);
        }

        p_tag++;
    }

    neu_json_elem_t resp_elems[] = { {
                                         .name         = "values",
                                         .t            = NEU_JSON_OBJECT,
                                         .v.val_object = values,
                                     },
                                     {
                                         .name         = "errors",
                                         .t            = NEU_JSON_OBJECT,
                                         .v.val_object = errors,

                                     } };

    ret = neu_json_encode_field(json_object, resp_elems,
                                NEU_JSON_ELEM_SIZE(resp_elems));
    return ret;
}

int neu_json_decode_write_req(char *buf, neu_json_write_req_t **result)
{
    int                   ret      = 0;
    void *                json_obj = NULL;
    neu_json_write_req_t *req      = calloc(1, sizeof(neu_json_write_req_t));
    if (req == NULL) {
        return -1;
    }

    json_obj = neu_json_decode_new(buf);

    neu_json_elem_t req_elems[] = {
        {
            .name = "node_name",
            .t    = NEU_JSON_STR,
        },
        {
            .name = "group_name",
            .t    = NEU_JSON_STR,
        },
        {
            .name = "tag_name",
            .t    = NEU_JSON_STR,
        },
        {
            .name = "value",
            .t    = NEU_JSON_VALUE,
        },
    };
    ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
                                  req_elems);
    if (ret != 0) {
        goto decode_fail;
    }

    req->node_name  = req_elems[0].v.val_str;
    req->group_name = req_elems[1].v.val_str;
    req->tag_name   = req_elems[2].v.val_str;
    req->t          = req_elems[3].t;
    req->value      = req_elems[3].v;

    *result = req;
    goto decode_exit;

decode_fail:
    if (req != NULL) {
        free(req);
    }
    ret = -1;

decode_exit:
    if (json_obj != NULL) {
        neu_json_decode_free(json_obj);
    }
    return ret;
}

void neu_json_decode_write_req_free(neu_json_write_req_t *req)
{
    free(req->group_name);
    free(req->node_name);
    free(req->tag_name);
    if (req->t == NEU_JSON_STR) {
        free(req->value.val_str);
    }

    free(req);
}

int neu_json_decode_read_req(char *buf, neu_json_read_req_t **result)
{
    int                  ret      = 0;
    void *               json_obj = NULL;
    neu_json_read_req_t *req      = calloc(1, sizeof(neu_json_read_req_t));
    if (req == NULL) {
        return -1;
    }

    json_obj = neu_json_decode_new(buf);

    neu_json_elem_t req_elems[] = { {
                                        .name = "node_name",
                                        .t    = NEU_JSON_STR,
                                    },
                                    {
                                        .name = "group_name",
                                        .t    = NEU_JSON_STR,
                                    } };
    ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
                                  req_elems);
    if (ret != 0) {
        goto decode_fail;
    }

    req->node_name  = req_elems[0].v.val_str;
    req->group_name = req_elems[1].v.val_str;

    *result = req;
    goto decode_exit;

decode_fail:
    if (req != NULL) {
        free(req);
    }
    ret = -1;

decode_exit:
    if (json_obj != NULL) {
        neu_json_decode_free(json_obj);
    }
    return ret;
}

void neu_json_decode_read_req_free(neu_json_read_req_t *req)
{
    free(req->group_name);
    free(req->node_name);

    free(req);
}

int neu_json_encode_read_periodic_resp(void *json_object, void *param)
{
    int                       ret  = 0;
    neu_json_read_periodic_t *resp = (neu_json_read_periodic_t *) param;

    neu_json_elem_t resp_elems[] = { {
                                         .name      = "node_name",
                                         .t         = NEU_JSON_STR,
                                         .v.val_str = resp->node_name,
                                     },
                                     {
                                         .name      = "group_name",
                                         .t         = NEU_JSON_STR,
                                         .v.val_str = resp->group_name,
                                     },
                                     {
                                         .name      = "timestamp",
                                         .t         = NEU_JSON_INT,
                                         .v.val_int = resp->timestamp,
                                     } };
    ret = neu_json_encode_field(json_object, resp_elems,
                                NEU_JSON_ELEM_SIZE(resp_elems));

    return ret;
}
